package com.yyh.stu;

import java.util.*;

/*集合和排序】
使用集合和排序实现学生信息管理。
学生基本信息：编号，姓名，年龄，成绩
功能实现：
1，创建学生类
2，创建管理类
功能列表包含：查询，添加，修改，删除，排序等功能
3，管理类数据初始化：创建集合容器，并通过方法向集合容器添加至少5名学生信息
4，查询所有学生信息
5，修改学生成绩
6，删除学生信息
7，对学生信息按年龄从小到大排序
8，对学生信息按成绩从高到低排序*/
public class Test5 {
    public static void main(String[] args) {
        StudentManager manager = new StudentManager( new ArrayList<>());
        Scanner scanner = new Scanner(System.in);
            while (true) {
                System.out.println("=========学生管理系统=============");
                System.out.println("1. 查询所有学生信息");
                System.out.println("2. 添加学生信息");
                System.out.println("3. 修改学生成绩");
                System.out.println("4. 删除学生信息");
                System.out.println("5. 按年龄从小到大排序");
                System.out.println("6. 按成绩从高到低排序");
                System.out.println("7. 按学号查询学生信息");
                System.out.println("0. 退出系统");
                System.out.print("请输入您的选择：");
                int choice = scanner.nextInt();
                scanner.nextLine(); // 消耗换行符
                switch (choice) {
                    case 1:
                        manager.queryAllStudent(); // 修改方法名
                        break;
                    case 2:
                        System.out.print("请输入学号：");
                        int newId = scanner.nextInt();
                        scanner.nextLine(); // 消耗换行符
                        System.out.print("请输入姓名：");
                        String newName = scanner.nextLine();
                        System.out.print("请输入年龄：");
                        int newAge = scanner.nextInt();
                        System.out.print("请输入成绩：");
                        int newScore = scanner.nextInt();
                        scanner.nextLine(); // 消耗换行符
                        manager.addStudent(new Student2(newId, newName, newAge, newScore));
                        break;
                    case 3:
                        System.out.print("请输入要修改的学号：");
                        int updateId = scanner.nextInt();
                        scanner.nextLine(); // 消耗换行符
                        System.out.print("请输入新成绩：");
                        int updateScore = scanner.nextInt();
                        scanner.nextLine(); // 消耗换行符
                        manager.updateStudentScore(updateId, updateScore);
                        break;
                    case 4:
                        System.out.print("请输入要删除的学号：");
                        int deleteId = scanner.nextInt();
                        scanner.nextLine(); // 消耗换行符
                        manager.deleteStudent(deleteId);
                        break;
                    case 5:
                        manager.sortByAge();
                        System.out.println("按年龄排序完成！");
                        manager.queryAllStudent();
                        break;
                    case 6:
                        manager.sortByScore();
                        System.out.println("按成绩排序完成！");
                        manager.queryAllStudent();
                        break;
                    case 7:
                        System.out.print("请输入要查询的学号：");
                        int queryId = scanner.nextInt();
                        scanner.nextLine(); // 消耗换行符
                        manager.queryStudent(queryId);
                        break;
                    case 0:
                        System.out.println("感谢使用学生信息管理系统！");
                        scanner.close();
                        return;
                    default:
                        System.out.println("无效选择，请重新输入！");
                }
        }
    }
}
//创建Student类
class Student2{
    //属性
    // 编号
    private int id1;
    // 姓名
    private String name1;
    // 年龄
    private int age1;
    // 成绩
    private int score1;
    // 无参构造方法
    public Student2() {
    }
    // 有参构造方法
    public Student2(int id1, String name1, int age1, int score1) {
        this.id1 = id1;
        this.name1 = name1;
        this.age1 = age1;
        this.score1 = score1;
    }
    // getter和setter方法
    public int getId1() {
        return id1;
    }
    public void setId1(int id1) {
        this.id1 = id1;
    }
    public String getName1() {
        return name1;
    }
    public void setName1(String name1) {
        this.name1 = name1;
    }
    public int getAge1() {
        return age1;
    }
    public void setAge1(int age1) {
        this.age1 = age1;
    }
    public int getScore1() {
        return score1;
    }
    public void setScore1(int score1) {
        this.score1 = score1;
    }
    //重写equals和hashCode方法
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student2 student2 = (Student2) o;
        return id1 == student2.id1 && age1 == student2.age1 && score1 == student2.score1 && Objects.equals(name1, student2.name1);
    }
    @Override
    public int hashCode() {
        return Objects.hash(id1, name1, age1, score1);
    }
    //toString方法
    @Override
    public String toString() {
        return "Student2{" +
                "id1=" + id1 +
                ", name1='" + name1 + '\'' +
                ", age1=" + age1 +
                ", score1=" + score1 +
                '}';
    }
}
//创建管理类
class StudentManager{
    private List<Student2> student2s;
    public StudentManager(List<Student2> student2s) {
        this.student2s = student2s;
        inital();
    }
    // 管理类初始化数据
    public void inital() {
        student2s.add(new Student2(1, "张三", 18, 90));
        student2s.add(new Student2(2, "李四", 19, 80));
        student2s.add(new Student2(3, "王五", 17, 70));
        student2s.add(new Student2(4, "赵六", 16, 60));
        student2s.add(new Student2(5, "七七", 15, 50));
        student2s.add(new Student2(6, "八八", 14, 40));
    }
    // 添加学生
    public void addStudent(Student2 student2) {
        student2s.add(student2);
        System.out.println("添加成功！");
        System.out.println(student2);
    }
    // 查询所有学生信息 - 修改方法名与调用一致
    public void queryAllStudent() {
        System.out.println("所有学生信息如下：");
        if (student2s.isEmpty()) {
            System.out.println("暂无学生信息！");
            return;
        }
        for (Student2 student2 : student2s) {
            System.out.println(student2);
        }
    }
    // 根据学号查找学生
    public Student2 queryStudentById(int id) {
        for (Student2 student2 : student2s) {
            if (student2.getId1() == id) {
                return student2;
            }
        }
        return null;
    }
    // 修改学生成绩
    public void updateStudentScore(int id, int score) {
        Student2 student2 = queryStudentById(id);
        if (student2 != null) {
            student2.setScore1(score);
            System.out.println("修改成功！");
            System.out.println(student2);
        } else {
            System.out.println("修改失败！未找到学号为" + id + "的学生！");
        }
    }
    // 删除学生信息
    public void deleteStudent(int id) {
        Student2 student2 = queryStudentById(id);
        if (student2 != null) {
            student2s.remove(student2);
            System.out.println("删除成功：" + student2.getName1());
        } else {
            System.out.println("删除失败！未找到学号为" + id + "的学生！");
        }
    }
    // 按年龄从小到大排序
    public void sortByAge() {
        student2s.sort(new Comparator<Student2>() {
            @Override
            public int compare(Student2 o1, Student2 o2) {
                return o1.getAge1() - o2.getAge1();
            }
        });
    }
    // 按成绩从高到低排序
    public void sortByScore() {
        student2s.sort(new Comparator<Student2>() {
            @Override
            public int compare(Student2 o1, Student2 o2) {
                return o2.getScore1() - o1.getScore1();
            }
        });
    }
    // 查询学生信息（按学号）
    public void queryStudent(int id) {
        Student2 student2 = queryStudentById(id);
        if (student2 != null) {
            System.out.println("查询成功！");
            System.out.println(student2);
        } else {
            System.out.println("查询失败！未找到学号为" + id + "的学生！");
        }
    }
    // 添加缺失的displayAllStudents方法（作为queryAllStudent的别名）
    public void displayAllStudents() {
        queryAllStudent();
    }
}
